package com.yida.util;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.*;
import java.text.MessageFormat;

public class emailHtml {

    //博客的谁回复谁的邮件提醒
    public static String buildUserHtml(String imgurl,String younickneme,String youcomment,String sonnickneme,String soncomment,String blogid) {
        //加载邮件html模板

        Resource resource = new ClassPathResource("templates/usermail.ftl");
        InputStream inputStream = null;
        BufferedReader fileReader = null;
        StringBuffer buffer = new StringBuffer();
        String line = "";
        try {
            inputStream = resource.getInputStream();
            fileReader = new BufferedReader(new InputStreamReader(inputStream));
            while ((line = fileReader.readLine()) != null) {
                buffer.append(line);
            }
        } catch (Exception e) {
            System.out.print("发送邮件读取模板失败{}");
        } finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //替换html模板中的参数
        return MessageFormat.format(buffer.toString(), imgurl,younickneme,youcomment,sonnickneme,soncomment,"https://taoyida.top/blog?id="+blogid);
    }

    //博客的评论邮件提醒
    public static String buildBloggerHtml(String imgurl,String younickneme,String sonnickneme,String soncomment,String blogid) {
        //加载邮件html模
        Resource resource = new ClassPathResource("templates/bloggermail.ftl");
        InputStream inputStream = null;
        BufferedReader fileReader = null;
        StringBuffer buffer = new StringBuffer();
        String line = "";
        try {
            inputStream = resource.getInputStream();
            fileReader = new BufferedReader(new InputStreamReader(inputStream));
            while ((line = fileReader.readLine()) != null) {
                buffer.append(line);
            }
        } catch (Exception e) {
            System.out.print("发送邮件读取模板失败{}");
        } finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //替换html模板中的参数
        return MessageFormat.format(buffer.toString(), imgurl,younickneme,sonnickneme,soncomment,"https://taoyida.top/blog?id="+blogid);
    }


    //留言的谁回复谁的邮件提醒
    public static String buildUserHtmlComment(String imgurl,String younickneme,String youcomment,String sonnickneme,String soncomment,String smileUrl) {
        //加载邮件html模板

        Resource resource = new ClassPathResource("templates/usermail.ftl");
        InputStream inputStream = null;
        BufferedReader fileReader = null;
        StringBuffer buffer = new StringBuffer();
        String line = "";
        try {
            inputStream = resource.getInputStream();
            fileReader = new BufferedReader(new InputStreamReader(inputStream));
            while ((line = fileReader.readLine()) != null) {
                buffer.append(line);
            }
        } catch (Exception e) {
            System.out.print("发送邮件读取模板失败{}");
        } finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //替换html模板中的参数
        return MessageFormat.format(buffer.toString(), imgurl,younickneme,youcomment,sonnickneme,soncomment,smileUrl);
    }

    //留言的评论邮件提醒
    public static String buildBloggerHtmlComment(String imgurl,String younickneme,String sonnickneme,String soncomment,String smileUrl) {
        //加载邮件html模
        Resource resource = new ClassPathResource("templates/bloggermail.ftl");
        InputStream inputStream = null;
        BufferedReader fileReader = null;
        StringBuffer buffer = new StringBuffer();
        String line = "";
        try {
            inputStream = resource.getInputStream();
            fileReader = new BufferedReader(new InputStreamReader(inputStream));
            while ((line = fileReader.readLine()) != null) {
                buffer.append(line);
            }
        } catch (Exception e) {
            System.out.print("发送邮件读取模板失败{}");
        } finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //替换html模板中的参数

        return MessageFormat.format(buffer.toString(), imgurl,younickneme,sonnickneme,soncomment,smileUrl);
    }
}
